﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;

using DaphneControls.ViewModel;
using DaphneProfiBase;


namespace DaphneControls
{
    /// <summary>
    /// Interaction logic for PDNTagEditor.xaml
    /// </summary>
    public partial class PDNTagEditor : UserControl
    {
        public PDNTagEditor()
        {
            InitializeComponent();
            this.LayoutUpdated += PDNTagEditor_LayoutUpdated;
        }

        void PDNTagEditor_LayoutUpdated(object sender, EventArgs e)
        {
            try
            {
                Point point = uxAddButton.PointToScreen(new Point(0, 0));

                PresentationSource source = PresentationSource.FromVisual(this);
                System.Windows.Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(point);

                ButtonPosition butPos = new ButtonPosition();
                butPos.X = targetPoints.X;
                butPos.Y = targetPoints.Y;
                butPos.Width = uxAddButton.ActualWidth;
                butPos.Height = uxAddButton.ActualHeight;

                // send the button position to the viewmodel
                Messenger.Default.Send<NotificationMessage<ButtonPosition>>
                    (new NotificationMessage<ButtonPosition>(butPos, "AddButtonPosition"));
            }
            catch
            {
            }
        }

        private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            PDNTagRecord rec = e.Row.Item as PDNTagRecord;

            if (e.EditingElement is TextBox)
            {
                rec.TagContent = (e.EditingElement as TextBox).Text;
                
                // send notification
                Messenger.Default.Send<NotificationMessage<PDNTagRecord>>(
                    new NotificationMessage<PDNTagRecord>(rec, Notify.PDNTagUpdated));
            }

            e.Cancel = false;

        }
    }
}

